#include<bits/stdc++.h>
#define int long long
#define inf 0x3f3f3f3f3f3f3f3f
#define cmin(a,b) a=min(a,b)
#define cmax(a,b) a=max(a,b)
#define rst(a,b) memset(a,b,sizeof(a))
#define ls p<<1
#define rs p<<1|1
#define lowbit(x) ((x)&-(x))

using namespace std;

const int N(2e5+5);

struct BIT1{
    int a[N],n;
    void init(int x){
        n=x;
        for(int i=1;i<=n;i++) a[i]=0;
    }
    void upd(int x,int v){
        for(int i=x;i<=n;i+=lowbit(i)){
            cmax(a[i],v);
        }
    }
    int query(int x){
        int res=0;
        for(int i=x;i;i-=lowbit(i)){
            cmax(res,a[i]);
        }
        return res;
    }
}dp;

int n,a[N],val[N],f[N],g[N]; //g[i]=dp[i][i-1]

void solve(){
    cin>>n;map <int,int> id;int tot=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(!id[a[i]]) val[id[a[i]]=++tot]=a[i],f[tot]=-inf;
        a[i]=id[a[i]];
    }
    dp.init(n);
    int tg=0;    
    for(int i=2;i<=n;i++){
        g[i]=max(dp.query(i-2),f[a[i]]+val[a[i]])+tg;
        tg+=(a[i]==a[i-1])*val[a[i]];
        dp.upd(i-1,g[i]-tg);
        cmax(f[a[i-1]],g[i]-tg);
    }
    cout<<dp.query(n)+tg<<'\n';
}

signed main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;cin>>t;
    while(t--) solve();

    return 0;
}